{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from time import time\n",
    "\n",
    "\n",
    "data = pd.read_csv(\"../data/cleaned_reviews.csv\").drop(\"Unnamed: 0\", axis=1)\n",
    "\n",
    "def add_user_reviews(x):\n",
    "    ur = user_reviews.loc[x[\"reviewerID\"]].drop(x[\"asin\"]).values.tolist()\n",
    "    mr = movie_reviews.loc[x[\"asin\"]].drop(x[\"reviewerID\"]).values.tolist()\n",
    "    x[\"userReviews\"] = \" \".join(list(map(lambda x: x[0], ur)))\n",
    "    x[\"movieReviews\"] = \" \".join(list(map(lambda x: x[0], mr)))\n",
    "    return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_item_review = data.drop(\"reviewText\", axis=1)\n",
    "user_reviews = pd.pivot_table(data,\n",
    "                              index=[\"reviewerID\", \"asin\"],\n",
    "                              aggfunc=lambda x: x).drop(\"overall\", axis=1)\n",
    "    \n",
    "movie_reviews = pd.pivot_table(data,\n",
    "                               index=[\"asin\", \"reviewerID\"],\n",
    "                               aggfunc=lambda x: x).drop(\"overall\", axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = pd.Timestamp(int(time()), unit=\"s\")\n",
    "grouped_cleaned_data = user_item_review.apply(add_user_reviews, axis=1)\n",
    "e = pd.Timestamp(int(time()), unit=\"s\")\n",
    "print(\"took {}\".format(e - s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grouped_cleaned_data.to_csv(\"../data/unembedded_grouped_cleaned_data.csv\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
